/**
 *
 *  @auth xlx_good@qq.com
 *  @date 17/6/21.
 *
 */

requirejs.config({
    baseUrl: '../../',
    paths:  {
        avalon: 'javascript/avalon.min',
        jquery: 'js/plugins/jquery/jquery-2.1.1.min',
        echarts: 'js/plugins/echarts/echarts-all',
        econfig: 'js/plugins/echarts/econfig',
        config: 'javascript/config',
        api: 'javascript/api',
        layer: 'js/plugins/layer1/layer'
    },
    shim: {
        api: {
            deps: ['jquery']
        },
        layer: {
            deps: ['jquery']
        }
    }
});


(function(win, dom, requirejs, initVm){
    var vm, myChart, data;
    requirejs(['avalon', 'jquery', 'echarts', 'econfig', 'config', 'api', 'layer'], function(){
        //初始化vm
        vm = initVm(dom);
		//获取主页数据
		data = getHomeData(vm);
    });

})(window, document, requirejs, function(dom){
    var vm = avalon.define({
        $id: 'container',
		total: {},
		saleList: [],
		href_1: function(){
			//window.location.href="../goods/onSale.html";
			window.parent.global.openPage({name: "在售商品", url: "page/goods/onSale.html"});
		},
		href_2: function(){
			//window.location.href="../statistics/day.html";
			window.parent.global.openPage({name: "日销售统计", url: "page/statistics/day.html"});
		},
		href_3: function(){
			//window.location.href="../order/waitDispatch.html";
			window.parent.global.openPage({name: "代发货订单", url: "page/order/waitDispatch.html"});
		}
    })
    avalon.scan(dom.body);
    return vm;
});
function getHomeData(vm) {
	Api.home(function(resp) {
		if(resp.code == 0) {
			vm.total = resp.data.total;
			vm.saleList = resp.data.saleList;
			var timeArr = [];
			var moneyArr = [];
			for(var i=resp.data.saleList.length-1;i>=0;i--) {
				var sale = resp.data.saleList[i];
				timeArr.push(sale.time);
				moneyArr.push(sale.saleMoney);
			}
			option.xAxis[0].data = timeArr;
			option.series[0].data = moneyArr;
			//初始化折线图
			myChart = initLine();
		} else {
			layer.msg(resp.msg);
		}
	});
}
var option = {
    title: {
        text: "7日销量变化",
        x: "center"
    },
    legend: {
        x: 'center',
        y:"bottom",
        data: ["订单金额"]
    },
    xAxis: [
        {
            type: "category",
            name: "时间",
            splitLine: {show: false},
            data: []
        }
    ],
    yAxis: [
        {
            name: "销量(单位：元)"
        }
    ],
    tooltip : {
        trigger: 'axis'
    },
    series: [
        {
            name: "销量",
            type: "line",
            data: []
        }
    ]
};
function  initLine(){
    var myChart = echarts.init(document.getElementById('line'));
    myChart.setOption(option);
    myChart.on(econfig.EVENT.LEGEND_SELECTED, eCallback);
    return myChart;
}
function eCallback(param){
    console.log(param);
}